﻿using System.Linq;
using System.Web.Mvc;
using Kooboo.Globalization;
using Kooboo.Web.Mvc.Paging;
using Kooboo.Commerce.Contexts.Promotions;

namespace Kooboo.Web.Areas.Commerce.Controllers
{
    using Kooboo.Commerce;
    
    using System;
    using Kooboo.IoC;
    
    using Kooboo.Web.Mvc;

    
    public class PromotionRuleController : ManagementController
    {
        public ActionResult Index()
        {
            var list = this.Actor.QueryPromotionRules().AsEnumerable();

            ViewData["delete_action"] = this.Url.Action<PromotionRuleController>(i => i.Delete(null));
            ViewData["add_action"] = this.Url.Action<PromotionRuleController>(i => i.Create());
            ViewData["edit_action"] = this.Url.Action<PromotionRuleController>(i => i.Edit(UrlParameter<int>.Empty));

            return View("Index", list);
        }

        public ActionResult Create()
        {
            ViewData["index_action"] = this.Url.Action<PromotionRuleController>(i => i.Index());
            ViewData["update_action"] = this.Url.Action<PromotionRuleController>(i => i.Create(null,null));
            ViewData["settings_action"] = "#";
            ViewData["rules"] = this.Actor
                .QueryPromotionRules()        
                .AsEnumerable();
            

            var rule = ObjectContainer.CreateInstance<IPromotionRule>();

            return View(rule);
        }


        [HttpPost]
        public ActionResult Create(IPromotionRule rule,string next)
        {

            if (ModelState.IsValid)
            {
                this.Actor.Post(rule);

                if (next == "settings")
                {
                    return this.Redirect(this.Url.Action<PromotionRuleController>(i => i.Settings(rule.Id)));
                }
                else
                {
                    return this.Redirect(this.Url.Action<PromotionRuleController>(i=>i.Index()));
                }
                
            }
            else
            {
                return Json(new { Message = ModelState.GetErrorMessages() });
            }
           
        }

        public ActionResult Edit(int id)
        {
            var rule = this.Actor
                .QueryPromotionRules()
                .FirstOrDefault(i => i.Id == id);


            ViewData["rules"] = this.Actor
                .QueryPromotionRules()
                .AsEnumerable();

            ViewData["index_action"] = this.Url.Action<PromotionRuleController>(i => i.Index());
            ViewData["update_action"] = this.Url.Action<PromotionRuleController>(i => i.Update(null,null));
            ViewData["settings_action"] = this.Url.Action<PromotionRuleController>(i => i.Settings(id));
       


            return View(rule);
        }


        [HttpPost]
        public ActionResult Update(IPromotionRule rule,string next)
        {

            if (ModelState.IsValid)
            {
                this.Actor.Put(rule);

                if (next == "settings")
                {
                    return this.Redirect(this.Url.Action<PromotionRuleController>(i => i.Settings(rule.Id)));
                }
                else
                {
                    return this.Redirect(this.Url.Action<PromotionRuleController>(i=>i.Index()));
                }
            }
            else
            {
                return Json(new { Message = ModelState.GetErrorMessages() });
            }
            
        }

        [HttpPost]
        public ActionResult Delete(IPromotionRule rule)
        {
            if (ModelState.IsValid)
            {
                this.Actor.Delete(rule);
                return Json(true);
            }
            else
            {
                return Json(new { Message= ModelState.GetErrorMessages()});
            }
        }

        public ActionResult Settings(int id)
        {
            var rule = this.Actor
                .QueryPromotionRules()
                .FirstOrDefault(i => i.Id == id);

            var view = rule.FindPromotion().Views.Settings(rule);
            if (view == null)
            {
                return View("Settings", rule);
            }
            else
            {
                return view;
            }
        }


        public ActionResult Logs(int id)
        {
            //var rule = Services.Current.PromotionRules
            //    .FindAll().FirstOrDefault(i => i.Id == id);

            //return View(rule);
            return View();
        }

      
    }
}
